<?php

class App_Form_User_Register extends Zend_Form
{

    public function init()
    {
        parent::init();
        $this->setMethod('post');
        $this->setAction('');


        $email = new Zend_Form_Element_Text('email');
        $email->setRequired()
                ->setLabel(_('Email:'))

                ->setFilters(array(
                                  'StringToLower',
                                  'StringTrim'
                             ))
                ->addValidators(array(new Zend_Validate_EmailAddress(),
                                     new Lib_Form_Validate_DbHasNotRecord('\Catalog\Users', 'email')));

        $email->addErrorMessages(array(Zend_Validate_EmailAddress::INVALID => 'Input valid email.',
                                      Lib_Form_Validate_DbHasNotRecord::ERROR_RECORD_FOUND => 'Record with same email already exists.'));
        $this->addElement($email);

        $name = new Zend_Form_Element_Text('name');
        $name->setRequired()
                ->addFilter(new Zend_Filter_StripTags())
                ->setLabel('Your Name:');


        $this->addElement($name);

        $password = new Zend_Form_Element_Password('password');
        $password->setLabel(_('Password:'))
                ->addValidators(array(
                                     new Lib_Form_Validate_InputEquals('passwordConfirmation', _('Passwords do not match.'))
                                ))
                ->setRequired();

        $password->addValidators(array(new Zend_Validate_StringLength(0, 15)));
        $password->addErrorMessages(array(
                                         Zend_Validate_StringLength::INVALID => 'Password must be less then 15 symbols.',
                                         Lib_Form_Validate_InputEquals::NOT_MATCH => 'Passwords do not match.'
                                    ));
        $this->addElement($password);

        $passwordConfirmation = new Zend_Form_Element_Password('passwordConfirmation');
        $passwordConfirmation->setLabel(_('Password confirmation:'))
                ->addValidator(new Lib_Form_Validate_InputEquals('password', _('Passwords do not match.')), true);

        $passwordConfirmation->addErrorMessages(array(Lib_Form_Validate_InputEquals::NOT_MATCH => 'Passwords do not match.'));
        $this->addElement($passwordConfirmation);

        $imgDir = "../public/images/captcha/";

        //         check captcha path is writeable
        if (is_writable($imgDir)) {

            $captcha = new Zend_Form_Element_Captcha('captcha', array(
                                                                     'label' => _('Write symbols from image:'),
                                                                     'captcha' => 'Image',
                                                                     'captchaOptions' => array(
                                                                         'captcha' => 'Image',
                                                                         'wordLen' => 6,
                                                                         'timeout' => 300,
                                                                         'imgDir' => $imgDir,
                                                                         'imgUrl' => '/images/captcha',
                                                                         'suffix' => '.c.png',
                                                                         'font' => dirname(APPLICATION_PATH) .
                                                                                   "/assets/fonts/Glasten_Bold.ttf",
                                                                     )
                                                                ));
            $view = $this->getView();
                        $view->captchaimage = '1';
        } else {
            $captcha = new Zend_Form_Element_Captcha('captcha', array(
                                                                     'label' => "Write symbols from image:",
                                                                     'captcha' => 'Figlet',
                                                                     'captchaOptions' => array(
                                                                         'wordLen' => 6,
                                                                         'timeout' => 300,
                                                                     ),
                                                                ));

        }

        $captcha->setAttrib('class','fixforcaptha');

        $this->addElement($captcha);

        $submit = new Zend_Form_Element_Submit('submit', array(
                                                              'label' => _('Register')
                                                         ));
        $submit->setAttrib('class', 'btn');
        $submit->setDecorators(array('ViewHelper'));
        $this->addElement($submit);


        $this->addElement('hash', 'csrf', array(
            'ignore' => true,
        ));

        return $this;
    }

}
